/*
 * version May 27, 2011 Harman created constructor and parse method
 * version May 28, 2011 Harman changed myClasses to be of type ClassPreference.
 * version June 1, 2011 Thach changed buffered Reader to Scanner, now throws FNFE instead of IOE.
 */
package parsers;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;

import data.ClassPreference;

/**
 * Parses a .txt file and creates a set of
 * ClassPreference objects
 * @author Harman
 * @version June 01, 2011
 */
public class ClassParser {
	private static final String SEPARATOR = ",";
	private Set<ClassPreference> myClasses;
	
	/**
	 * Parses a file for a set of ClassPreferences
	 * @pre theFile is formatted appropriately. Specifically 
	 * each line is of the form:
	 * class number,class title,class section,class credits
	 * @pre theFile != null
	 * @post creates a set of classes
	 * @param theFile
	 * @throws FileNotFoundException
	 */
	public ClassParser(final File theFile) throws FileNotFoundException {
		myClasses = new HashSet<ClassPreference>();
		parse(theFile);
	}
	
	/**
	 * @param theFile
	 * @throws FileNotFoundException
	 */
	public void parse(final File theFile) throws FileNotFoundException  {
		Scanner scan = new Scanner(theFile);
		while (scan.hasNextLine()) {
			StringTokenizer strToken = new StringTokenizer(scan.nextLine(), SEPARATOR);
			String num = strToken.nextToken();
			String title = strToken.nextToken();
			char section = strToken.nextToken().charAt(0);
			int credits = Integer.parseInt(strToken.nextToken());
			myClasses.add(new ClassPreference(num, title, section, credits));
		}
	}
	
	public Set<ClassPreference> getClasses()
	{
		return myClasses;
	}
}
